import java.util.ArrayList;



public class Borrowable extends ItemDecorator
{
	public Borrowable(Items i)
	{
		super(i);
	}
	public Item borrowItem(CustomerAccount account)
	{
		if(items.getCustomerQueue().isEmpty())
		{
			for(int i=0; i<items.getItemList().size(); i++)
			{
				if(items.getItemList().get(i).outgoing(account))
				{
					items.itemOut();
					System.out.println("Borrow successful.\nItem id number:"+items.getItemList().get(i).getID());
					return items.getItemList().get(i);
				}
			}
			ArrayList<CustomerAccount> q=items.getCustomerQueue();
			q.add(account);
			items.setCustomerQueue(q);
			
			ArrayList<Boolean> b=items.getNotified();
			b.add(false);
			items.setNotified(b);
			System.out.println("Borrow failed, added to queue");
			
		}
		else if(items.getCustomerQueue().indexOf(account)!=-1 &&items.getNotified().get(items.getCustomerQueue().indexOf(account)))
		{
			for(int i=0; i<items.getItemList().size(); i++)
			{
				if(items.getItemList().get(i).outgoing(account))
				{
					items.itemOut();
					System.out.println("Borrow successful.\nItem id number:"+items.getItemList().get(i).getID());
					int index=items.getCustomerQueue().indexOf(account);
					items.getCustomerQueue().remove(index);
					items.getNotified().remove(index);
					return items.getItemList().get(i);
				}
			}
		}
		else
		{
			System.out.println("There is a queue, you have been added to the queue.");
			
			ArrayList<CustomerAccount> q=items.getCustomerQueue();
			q.add(account);
			items.setCustomerQueue(q);
			
			ArrayList<Boolean> b=items.getNotified();
			b.add(false);
			items.setNotified(b);
		}
			
		return null;
	}
	public boolean returnItem(int id)
	{
		if(!items.getCustomerQueue().isEmpty())
		{
			for(int i=0; i<items.getNotified().size(); i++)
			{
				if(!items.getNotified().get(i))
				{
					items.getCustomerQueue().get(i).sendNotification();
					ArrayList<Boolean> asdf=items.getNotified();
					asdf.set(i, true);
					items.setNotified(asdf);
				}
			}
		}
		for(int i=0; i<items.getItemList().size(); i++)
		{
			if(items.getItemList().get(i).getID()==id)
			{
				if(items.getItemList().get(i).incoming())
				{
					System.out.println("Item successfully returned.");
					return true;
				}
			}
		}
		return false;
	}
	public double getPrice(Item i)
	{
		return i.getPrice();
	}
}
